#!/bin/bash
# date: 2024/06/25

# 代理
repoproxy=m.daocloud.io
# 修改为本地仓库
repo=hub.abc.com/basic

manifests_install() {
   kubectl apply --server-side -f manifests/setup
   kubectl wait \
      --for condition=Established \
      --all CustomResourceDefinition \
      --namespace=monitoring

   kubectl api-resources | grep monitoring.coreos.com

   kubectl apply -f manifests/
}

manifests_uninstall() {
   kubectl delete --ignore-not-found=true -f manifests/ -f manifests/setup
}

image_pull_repo() {
   grep -Er "image: [a-z]*" manifests/ | awk '{print $NF}' | sort | uniq >image.txt
   grep -r 'quay.io/prometheus-operator/prometheus-config-reloader' manifests/ | awk -F"=" '{print $NF}' >>image.txt
   echo "quay.io/thanos/thanos:v0.25.1" >>image.txt

   while read line; do
      oldimage=$line
      name=$(echo $line | awk -F/ '{print $NF}')
      newimage=$(echo "$repo/$name")
      docker pull ${repoproxy}/${oldimage}
      docker tag ${repoproxy}/${oldimage} $newimage
      docker push $newimage
   done <image.txt

   docker pull quay.io/coreos/kube-state-metrics:v1.9.8
   docker tag quay.io/coreos/kube-state-metrics:v1.9.8 $repo/kube-state-metrics:v1.9.8
   docker push $$repo/kube-state-metrics:v1.9.8

   # docker pull quay.io/coreos/k8s-prometheus-adapter-amd64:v0.8.4
   # docker tag quay.io/coreos/k8s-prometheus-adapter-amd64:v0.8.4 $repo/prometheus-adapter:v0.8.4
   # docker push $repo/prometheus-adapter:v0.8.4
}

image_replace() {
   # dos2unix image.txt

   while read line; do
      oldimage=$line
      name=$(echo $line | awk -F/ '{print $NF}')
      newimage=$(echo "$repo/$name")
      sed -i "s#$oldimage#$newimage#g" prometheusOperator/prometheusOperator-deployment.yaml
      sed -i "s#$oldimage#$newimage#g" prometheus/prometheus-prometheus.yaml
      sed -i "s#$oldimage#$newimage#g" prometheus/thanos/thanos-query-deployment.yaml
      sed -i "s#$oldimage#$newimage#g" prometheus/thanos/thanos-store-statefulset.yaml
      sed -i "s#$oldimage#$newimage#g" alertmanager/alertmanager-alertmanager.yaml
      sed -i "s#$oldimage#$newimage#g" grafana/grafana-deployment.yaml
      sed -i "s#$oldimage#$newimage#g" nodeExporter/nodeExporter-daemonset.yaml
      sed -i "s#$oldimage#$newimage#g" blackboxExporter/blackboxExporter-deployment.yaml
      sed -i "s#$oldimage#$newimage#g" kubeStateMetrics/kubeStateMetrics-deployment.yaml
   done <image.txt

   sed -i "s#hub.abc.com/basic/webhook:v0.0.1#$repo/webhook:v0.0.1#g" webhook/webhook-deployment.yaml
   sed -i "s#registry.k8s.io/kube-state-metrics/kube-state-metrics:v2.9.2#$repo/kube-state-metrics:v1.9.8#g" kubeStateMetrics/kubeStateMetrics-deployment.yaml

   # sed -i "s#registry.k8s.io/prometheus-adapter/prometheus-adapter:v0.12.0#$repo/prometheus-adapter:v0.8.4#g" manifests/prometheusAdapter-deployment.yaml
}

install() {
   # prometheusOperator
   kubectl apply -f manifests/setup/namespace.yaml
   kubectl apply --server-side -f prometheusOperator/crd
   kubectl apply -f prometheusOperator/

   # prometheus
   kubectl apply -f prometheus/
   # kubectl apply -f prometheus/thanos/
   kubectl apply -f prometheusOperator/

   # alertmanager
   kubectl apply -f alertmanager/
   kubectl apply -f webhook/
   kubectl apply -f alertmanager-wechatrobot-webhook/wechat-webhook.yaml

   # grafana
   kubectl apply -f grafana/

   # nodeExporter
   kubectl apply -f nodeExporter/

   # blackboxExporter
   kubectl apply -f blackboxExporter/

   # kubeStateMetrics
   kubectl apply -f kubeControlPlane/
   kubectl apply -f kubeStateMetrics/

   ## prometheus scrape object
   kubectl apply -f serviceMonitor/prometheus-serviceMonitor.yaml
   kubectl apply -f serviceMonitor/alertmanager-serviceMonitor.yaml
   kubectl apply -f serviceMonitor/grafana-serviceMonitor.yaml
   kubectl apply -f serviceMonitor/nodeExporter-serviceMonitor.yaml
   kubectl apply -f serviceMonitor/kubeStateMetrics-serviceMonitor.yaml
   kubectl apply -f serviceMonitor/kubernetesControlPlane-serviceMonitorKubelet.yaml
   kubectl apply -f serviceMonitor/kubernetesControlPlane-serviceMonitorApiserver.yaml
   kubectl apply -f serviceMonitor/kubernetesControlPlane-serviceMonitorKubeControllerManager.yaml
   kubectl apply -f serviceMonitor/kubernetesControlPlane-serviceMonitorKubeScheduler.yaml
   kubectl apply -f serviceMonitor/

   ## prometheus alert & record rules
   kubectl apply -f prometheusRule/prometheus-prometheusRule.yaml
   kubectl apply -f prometheusRule/alertmanager-prometheusRule.yaml
   kubectl apply -f prometheusRule/grafana-prometheusRule.yaml
   kubectl apply -f prometheusRule/nodeExporter-serviceMonitor.yaml
   kubectl apply -f prometheusRule/kubeStateMetrics-prometheusRule.yaml
   kubectl apply -f prometheusRule/kubernetesControlPlane-prometheusRule.yaml
   kubectl apply -f prometheusRule/
}

uninstall() {
   kubectl delete -f manifests/setup/namespace.yaml
   # 删除pvc
   kubectl -n monitoring delete pvc thanos-store-thanos-store-0
   kubectl -n monitoring delete pvc thanos-store-thanos-store-1
}

kube_ls() {
   kubectl -n monitoring get all
}

case "${1}" in
image)
   # image_pull_repo
   image_replace
   ;;
install)
   install
   ;;
uninstall)
   uninstall
   ;;
ls)
   kube_ls
   ;;
*)
   echo "image|install|uninstall|ls"
   ;;
esac

# 替换本地仓库地址
# sed -i 's#repo=hub.abc.com/basic#repo=hub.abc.com/public#g' ctrl.sh
# sh ctrl.sh image
# sh ctrl.sh install
